package com.xh.jf_blog.controller.admin;

import com.xh.jf_blog.bean.Category;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2024/2/1/001
 */
@Controller
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public String list() {
        return "admin/category/category-list";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Category category) {
        return categoryService.getPage(page, limit, category);
    }

    @GetMapping("/edit")
    public String edit(Integer categoryId, Model model) {
        if (categoryId != null) {
            model.addAttribute("category", categoryService.getById(categoryId));
        }
        return "admin/category/category-update";
    }

    @PostMapping(value = {"/update", "/status"})
    @ResponseBody
    public R update(Category category) {
        categoryService.saveOrUpdate(category);
        return R.ok();
    }

    @DeleteMapping("/delete/{categoryId}")
    @ResponseBody
    public R delete(@PathVariable int categoryId) {
        categoryService.deleteById(categoryId);
        return R.ok();
    }
}
